﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.Net.Mail;

namespace ZooSystem
{
    /// <summary>
    /// Логика взаимодействия для AdminPanel.xaml
    /// </summary>
    public partial class AdminPanel : Window
    {
        Serialization database;
        UserData current;
        public AdminPanel()
        {
            InitializeComponent();         
            database = Serialization.getDatabase();

            foreach (UserData item in database.UsersList)
            {
                ChooseWorker_CmbBox.Items.Add(item);
            }

            UserType_LstBox.Items.Add(type.admin);
            UserType_LstBox.Items.Add(type.employee);
            UserType_LstBox.Items.Add(type.user);
        }

        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            Environment.Exit(1);
        }

        private void SetWorker_Btn_Click(object sender, RoutedEventArgs e)
        {
            current = (UserData)ChooseWorker_CmbBox.SelectedItem;

            current.setUsers_type((type)UserType_LstBox.SelectedItem);

            database.SaveUsers();
        }

        private void ResetPassword_Btn_Click(object sender, RoutedEventArgs e)
        {
            Random rnd = new Random((int)DateTime.Now.Ticks);
            StringBuilder builder = new StringBuilder();
            char ch;
            for (int i = 0; i < 7; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65)));                 
                builder.Append(ch);
            }

            String newPass;
            newPass = builder.ToString();

            MailMessage mail = new MailMessage("zoosystempaswordreset@gmail.com", current.email, "Password Changed", 
                "Your new password is:\n" + newPass);
            SmtpClient client = new SmtpClient("smtp.googlemail.com", 587);
            client.Port = 25;
            client.Credentials = new NetworkCredential("zoosystempaswordreset@gmail.com", 
                "ReliablePassword");
            client.EnableSsl = true;
            try
            {
                client.Send(mail);
            }
            catch (SmtpException err)
            {
                MessageBox.Show("Mail not sent!" + "\n\n" + err.Message + 
                    "\n" + err.HelpLink, "Error", MessageBoxButton.OK);
                return;
            }

            MessageBox.Show("Mail sent!\n" + "New Pass: " + newPass, "Success!", MessageBoxButton.OK);
            current.password = newPass;
        }

        private void ChooseWorker_CmbBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ChooseWorker_CmbBox.SelectedItem == null)
            {
                SetWorker_Btn.IsEnabled = false;
                ResetPassword_Btn.IsEnabled = false;
                UserType_LstBox.IsEnabled = false;
            }
            else
            {
                SetWorker_Btn.IsEnabled = true;
                ResetPassword_Btn.IsEnabled = true;
                UserType_LstBox.IsEnabled = true;

                current = (UserData)ChooseWorker_CmbBox.SelectedItem;

                UserType_LstBox.SelectedItem = UserType_LstBox.Items.GetItemAt(
                    UserType_LstBox.Items.IndexOf(current.getUsers_type()));
            }
        }

        private void LogOut_Btn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        
    }
}
